package com.jytc.mh.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jytc.framework.commons.AdminLoginToken;
import com.jytc.framework.commons.Page;
import com.jytc.framework.commons.PropertyFilter;
import com.jytc.framework.constants.CommonConstants;
import com.jytc.framework.constants.ConfigConstants;
import com.jytc.framework.constants.RequestNameConstants;
import com.jytc.framework.constants.SessionNameConstants;
import com.jytc.framework.utils.DateTimeUtils;
import com.jytc.framework.utils.FileUtils;
import com.jytc.framework.utils.HibernateWebUtils;
import com.jytc.framework.utils.MultiFileUploader;
import com.jytc.framework.utils.SessionUtils;
import com.jytc.mh.bo.MhImgmgrBO;
import com.jytc.mh.dto.MhImgmgrDTO;

/**
 * 
 * @title: MhImgmgrController.java
 * @description: 门户图片管理
 * @author: dong_yse
 */
@Controller
@RequestMapping("/admin/jsp/imgMgr.do")
public class MhImgmgrController extends MultiFileUploader
{

	/**
	 * 日志
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 查询展示页面
	 */
	private final String QUERY_GOTO_URL = "/jytc/jsp/mh/imgMgr/img_mgr.jsp";

	/**
	 * 编辑页面
	 */
	private final String EDIT_GOTO_URL = "/jytc/jsp/mh/imgMgr/img_edit.jsp";

	/**
	 * 增加页面
	 */
	private final String ADD_GOTO_URL = "/jytc/jsp/mh/imgMgr/img_add.jsp";

	/**
	 * 查询RUL
	 */
	private final String VIEW_URL = "/admin/jsp/imgMgr.do?method=queryImg";

	/**
	 * 图片管理业务操作接口
	 */
	@Autowired
	private MhImgmgrBO mhImgmgrBO;

	/**
	 * 图片信息查询
	 * @param request 请求对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=queryImg")
	public ModelAndView queryImg(HttpServletRequest request, ModelMap model,
			Page page)
	{
		try
		{
			List<PropertyFilter> filters = HibernateWebUtils
					.buildPropertyFilters(request);
			Page pageResult = mhImgmgrBO.query(page, filters);
			model.addAttribute(RequestNameConstants.PAGE_OBJECT, pageResult);
		}
		catch (Exception e)
		{
			logger.error("图片查询异常", e);
		}
		return new ModelAndView(QUERY_GOTO_URL);
	}

	/**
	 * 图片新增初始化
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=addImg")
	public ModelAndView addImg(ModelMap model)
	{
		return new ModelAndView(ADD_GOTO_URL);
	}

	/**
	 * 保图片信息
	 * @param mhImgmgrDTO 图片信息数据传输对象
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=saveImg")
	public ModelAndView saveImg(HttpServletRequest request,
			MhImgmgrDTO mhImgmgrDTO, ModelMap model)
	{
		try
		{
			AdminLoginToken adminLoginToken = (AdminLoginToken) SessionUtils
					.getObjectAttribute(request,
							SessionNameConstants.ADMIN_LOGIN_TOKEN);
			List<MultipartFile> fileList = this.getFileList(request);
			String imgPath = null;
			if ((fileList != null) && (!fileList.isEmpty()))
			{
				CommonsMultipartFile cmf = (CommonsMultipartFile) fileList
						.get(0);
				String fileName = cmf.getFileItem().getName();

				// 获取图片后缀名
				String fileSuffix = fileName.substring(fileName
						.lastIndexOf("."), fileName.length());
				String basePath = ConfigConstants.MH_IMAGE;

				// 在图片资源路径下创建日期文件夹
				imgPath = DateTimeUtils.getChar8()
						+ CommonConstants.FILE_SEPARATOR
						+ System.currentTimeMillis() + fileSuffix;

				// 往服务器指定路径写入图片
				FileUtils.write(cmf.getBytes(), basePath + imgPath);
				mhImgmgrDTO.setImgPath(imgPath);
			}
			mhImgmgrDTO.setImgCreator(adminLoginToken.getFwUser().getUserId());
			mhImgmgrBO.save(mhImgmgrDTO);
		}
		catch (Exception e)
		{
			logger.error("图片新增异常", e);
			return new ModelAndView(ADD_GOTO_URL);
		}
		return new ModelAndView(VIEW_URL);
	}

	/**
	 * 按照图片ID删除图片信息
	 * @param imgId 图片ID
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delImg")
	public ModelAndView delImg(Integer imgId, ModelMap model)
	{
		try
		{
			mhImgmgrBO.deleteById(imgId);
		}
		catch (Exception e)
		{
			logger.error("图片删除异常", e);
		}
		return new ModelAndView(VIEW_URL);
	}

	/**
	 * 批量删除图片信息
	 * @param imgIds 图片ID集合
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delImgs")
	public ModelAndView delImgs(Integer[] imgIds, ModelMap model)
	{
		try
		{
			mhImgmgrBO.deleteByIds(imgIds);
		}
		catch (Exception e)
		{
			logger.error("图片批量删除异常", e);
		}
		return new ModelAndView(VIEW_URL);
	}

	@Override
	public boolean validateFile(MultipartFile file)
	{
		// TODO Auto-generated method stub
		return false;
	}
}
